package com.jl.basicframework.config;

import com.jl.basicframework.httpservice.HttpLoggingInterceptor;
import com.jl.basicframework.httpservice.RetrofitUtils;

/***
 * @author 作者:姜澜
 * 时间:2022/3/8
 * 邮箱:316879104@pp.com
 * 类简述:
 */
public class HttpConfig {
    /**
     * 生产路径
     */
    private final String PRODUCE_PATH = "";

    /**
     * 测试路径
     */
    private final String TEST_PATH = "";

    /**
     * 连接超时时间
     */
    public final static int CONNECT_TIME_OUT = 20;
    /**
     * 读取超时时间
     */
    public final static int READ_TIME_OUT = 20;
    /**
     * 写入超时时间
     */
    public final static int WRITE_TIME_OUT = 20;

    /**
     * debug标识
     */
    private boolean isDebug;


    public void initConfig(){
        //获取Retrofit单例对象
        RetrofitUtils iRetrofitUtils = RetrofitUtils.getInstances();
        iRetrofitUtils
                .setUrl(getPath())
                .setConnectTimeout(CONNECT_TIME_OUT)
                .setReadTimeout(READ_TIME_OUT)
                .setWriteTimeout(WRITE_TIME_OUT)
                //网络日志拦截器
                .addInterceptor(new HttpLoggingInterceptor()
                        //TODO 从BODY改成HEADERS解决重复调用问题
                        .setLevel(getLogLevel()))
                .initUtils();

    }

    /**
     * 返回主项目路径
     * @return
     */
    private String getPath(){
        if (isDebug){
            return  TEST_PATH;
        }else {
            return PRODUCE_PATH;
        }
    }

    /**
     * 返回日志拦截器等级
     */
    public HttpLoggingInterceptor.Level getLogLevel(){
        if (isDebug){
            return HttpLoggingInterceptor.Level.BODY;
        }else {
            return HttpLoggingInterceptor.Level.HEADERS;
        }
    }

    /**
     * 设置是否开始debug
     * @param isDebug
     */
    public HttpConfig isDebug(boolean isDebug) {
        this.isDebug = isDebug;
        return this;
    }
}
